package com.jzqf.harmonydemo.views.slice;

import com.jzqf.harmonydemo.ResourceTable;
import com.jzqf.harmonydemo.ability.*;
import com.jzqf.harmonydemo.base.BaseAbilitySlice;
import com.jzqf.harmonydemo.library.media.MediaPlayerAbility;
import com.jzqf.harmonydemo.model.MenuItem;
import com.jzqf.harmonydemo.provider.MenuProvider;
import com.jzqf.harmonydemo.utils.LogUtil;
import com.jzqf.harmonydemo.views.ButtonAbility;
import com.jzqf.harmonydemo.views.ComponentsAbility;
import com.jzqf.harmonydemo.views.PageSliderAbility;
import com.jzqf.harmonydemo.views.TextAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

/**
 * ListContainer列表
 *
 * @author LiuWeiHao
 * 2021/5/7 16:44
 */
public class ListContainerAbilitySlice extends BaseAbilitySlice {
    private final String TAG = ListContainerAbilitySlice.class.getSimpleName();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LogUtil.d(TAG, "onStart");
        hideBack();
        setTitle("HarmonyOS");
        initView();
    }

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_list_container;
    }

    private void initView() {
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer_list);
        //设置回弹效果
        listContainer.setReboundEffectParams(40, 0.5f, 20);
        MenuProvider menuProvider = new MenuProvider(this, getData());
        listContainer.setItemProvider(menuProvider);
//        menuProvider.notifyData(getData());//TODO 刷新后不起作用
        menuProvider.setItemChildClickedListener((componentId, position) -> {
            //点击条目页面跳转
            if (componentId == ResourceTable.Id_item_desc_btn) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withAbilityName(menuProvider.getItem(position).getAbilityName())
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
            }
        });
    }

    private List<MenuItem> getData() {
        List<MenuItem> list = new ArrayList<>();
        list.add(new MenuItem("视频播放", MediaPlayerAbility.class.getName()));
        list.add(new MenuItem("camera", CameraAbility.class.getName()));
        list.add(new MenuItem("设备信息", DeviceInfoAbility.class.getName()));
        list.add(new MenuItem("Thread", ThreadAbility.class.getName()));
        list.add(new MenuItem("ORM数据缓存", DatabaseAbility.class.getName()));
        list.add(new MenuItem("PageSlider", PageSliderAbility.class.getName()));
        list.add(new MenuItem("基础组件", ComponentsAbility.class.getName()));
        list.add(new MenuItem("Button", ButtonAbility.class.getName()));
        list.add(new MenuItem("Text", TextAbility.class.getName()));
        return list;
    }

    @Override
    public void onActive() {
        super.onActive();
        LogUtil.d(TAG, "onActive");
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        LogUtil.d(TAG, "onInactive");
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        LogUtil.d(TAG, "onForeground");
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        LogUtil.d(TAG, "onBackground");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtil.d(TAG, "onStop");
    }
}
